﻿// //------------------------------------------------------------------------------
// // <copyright file="" company="Dascoba Development">
// //  Copyright © Dascoba Development 2010
// // </copyright>
// //------------------------------------------------------------------------------
namespace Dascoba.Umb.FileManager.Dialogs
{
    using System;
    using System.Web;
    using System.Web.SessionState;

    using Dascoba.Umb.FileManager.Support;

    /// <summary>
    ///   This handler takes a posted file and saves it to the disk.
    /// </summary>
    /// <remarks>
    /// </remarks>
    public class UploadHandler : IHttpHandler, IRequiresSessionState
    {
        /// <summary>
        ///   Enables processing of HTTP Web requests by a custom HttpHandler that implements the <see cref = "T:System.Web.IHttpHandler" /> interface.
        /// </summary>
        /// <param name = "context">An <see cref = "T:System.Web.HttpContext" /> object that provides references to the intrinsic server objects (for example, Request, Response, Session, and Server) used to service HTTP requests.</param>
        public void ProcessRequest(HttpContext context)
        {
            // Run once per file selected

            var postedFile = context.Request.Files["Filedata"];
            try
            {
                var uploadPath = context.Server.UrlDecode(context.Request["uploadPath"]);

                var saveFilePath = Util.MapPath(string.Format("{0}/{1}", uploadPath, postedFile.FileName));
                postedFile.SaveAs(saveFilePath);

                context.Response.Write("success");
            }
            catch (Exception ex)
            {
                context.Response.Write(ex.Message);
                return;
            }
        }

        /// <summary>
        ///   Gets a value indicating whether another request can use the <see cref = "T:System.Web.IHttpHandler" /> instance.
        /// </summary>
        /// <returns>
        ///   true if the <see cref = "T:System.Web.IHttpHandler" /> instance is reusable; otherwise, false.
        /// </returns>
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}